//
//  ModuleItemCell.swift
//  demo01
//
//  Created by babybus on 2025/6/27.
//
import Foundation
import UIKit
import SnapKit
class ModuleItemCell: UICollectionViewCell {
    public var imageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        return iv
    }()
    
    public var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 12)
        label.textAlignment = .center
        label.adjustsFontSizeToFitWidth = true  // 开启自动缩放
        label.minimumScaleFactor = 0.25          // 最小缩放比例（默认0.5）
        label.numberOfLines = 1                 // 单行文本
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        let stackView = UIStackView()
        stackView.addArrangedSubview(imageView)
        stackView.addArrangedSubview(titleLabel)
        stackView.axis = .vertical
        stackView.alignment = .center
        
        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.width.height.equalToSuperview()
        }
        
        imageView.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.top.equalToSuperview().inset(0)
        }
        titleLabel.snp.makeConstraints { make in
            make.height.equalTo(15)
            make.top.equalTo(imageView.snp.bottom).offset(6)
        }
    }
    
    func configure(with item: ModuleInfo) {
        imageView.image = UIImage(named: item.moduleImg!)
        titleLabel.text = item.moduleName
    }
}
